package com.example.administrator.easy.detailxinfo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.DetailVideoAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jingsheng on 2016/11/15.
 */

public class FragmentVideo extends Fragment {
    private String id;
    private int type;

    private ListView listView;

    private DetailVideoAdapter adapter;

    private List<DetailVideoBean.ResultBean> list;
    private DetailVideoBean.ResultBean.LiveAnnexListBean liveAnnexListBean;
    private  DetailVideoBean.ResultBean bean;
    private List<DetailVideoBean.ResultBean.LiveAnnexListBean> ml;

    private String tempUrl="http://m.ieasy.tv/api/livechat/unreadlive/46543/422834";
    private String chat="http://m.ieasy.tv/api/livechat/list/10/46543/0";
    private String temp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_video_show,null);

        initView(view);
        intId();
        initData();
        setData();
        return view;
    }

    private void initView(View view) {
        listView= (ListView) view.findViewById(R.id.video_fragment_lv);
    }

    private void setData() {
        listView.setAdapter(adapter);
    }

    private void initData() {

        list=new ArrayList<>();

        adapter=new DetailVideoAdapter(getActivity(),list);
        RequestParams params=new RequestParams(id);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONObject object=new JSONObject(result);
                    JSONArray jsonArray = object.optJSONArray("result");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        bean=new DetailVideoBean.ResultBean();
                        JSONObject object1 = jsonArray.optJSONObject(i);
                        String chatContent = object1.optString("ChatContent");
                        String userAvatar = object1.optString("UserAvatar");
                        String userNickName = object1.optString("UserNickName");
                        JSONArray jsonArray1 = object1.optJSONArray("LiveAnnexList");

                        bean.setChatContent(chatContent);
                        bean.setUserAvatar(userAvatar);
                        bean.setUserNickName(userNickName);

                        ml=new ArrayList<DetailVideoBean.ResultBean.LiveAnnexListBean>();
                            for (int j = 0; j < jsonArray1.length(); j++) {

                                liveAnnexListBean=new DetailVideoBean.ResultBean.LiveAnnexListBean();
                                JSONObject object2 = jsonArray1.optJSONObject(j);
                                int annexType = object2.optInt("AnnexType");
                                String annexUrl = object2.optString("AnnexUrl");
                                String duration = object2.optString("Duration");
                                String videoCover = object2.optString("VideoCover");
                                liveAnnexListBean.setDuration(duration);
                                liveAnnexListBean.setAnnexId(annexType);
                                liveAnnexListBean.setVideoCover(videoCover);
                                ml.add(liveAnnexListBean);

                            }
                        bean.setLiveAnnexList(ml);

                        list.add(bean);
                    }

                    adapter.notifyDataSetChanged();
                    Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "onError", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void intId() {
        Bundle bundle=getArguments();
        id=bundle.getString("id");
        Log.e("id",id);

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
}
